#include <Arduino.h>
#include "ESP8266WiFi.h"
#include <ArduinoOTA.h>
#include "ws2812.h"
#include <ESP8266WebServer.h>

const char* ssid="501 Master 2.4Ghz";
const char* password="1999052912";
uint8_t mode = 0;

//网页
String myhtmlPage =
    String("") +
    "<html>" +
    "<head>" +
    "    <title>ESP8266 Web Server Test</title>" +
    "    <script defer=\"defer\">" +
    "        function ledSwitch() {" +
    "            var xmlhttp;" +
    "            if (window.XMLHttpRequest) {" +
    "                xmlhttp = new XMLHttpRequest();" +
    "            } else {" +
    "                xmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");" +
    "            }" +
    "            xmlhttp.onreadystatechange = function () {" +
    "                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {" +
    "                    document.getElementById(\"txtState\").innerHTML = xmlhttp.responseText;" +
    "                }" +
    "            }," +
    "            xmlhttp.open(\"GET\", \"Switch\", true);" +
    "            xmlhttp.send(); " +
    "        }" +
    "    </script>" +
    "</head>" +
    "<body>" +
    "    <div id=\"txtState\">Mode</div>" +
    "    <input type=\"button\" value=\"Switch\" οnclick=\"ledSwitch()\">" +
    "</body>" +
    "</html>";

bool isLedTurnOpen = false; // 记录LED状态
ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/html", myhtmlPage); //！！！注意返回网页需要用"text/html" ！！！
  Serial.println("用户访问了主页");
}

void handleSwitch() {
  if (isLedTurnOpen == false) {
    // digitalWrite(2, LOW); // 点亮LED
    mode = 1;
    server.send(200, "text/plain", "LED has been turn on");
    Serial.println("用户开启了LED");
    isLedTurnOpen = true;
  }
  else {
    // digitalWrite(2, HIGH); // 熄灭LED
    mode = 2;
    server.send(200, "text/plain", "LED has been turn off");
    Serial.println("用户关闭了LED");
    isLedTurnOpen = false;
  }                      
}

void handleNotFound() {
  server.send(404, "text/plain", "嘤嘤嘤！你访问的页面被外星人劫持了哦");
  Serial.println("用户访问了一个不存在的页面");
}

void setup() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  ws2812_init();
  while (WiFi.status() != WL_CONNECTED){
    flow_wifi();
  }
  ArduinoOTA.begin();

    //初始化WebServer
  server.on("/", handleRoot);
  server.on("/Switch", handleSwitch);
  server.onNotFound(handleNotFound);
  server.begin();
}

void loop() {
  // static bool flag_mode = 0;
  // static uint8_t mode = 0;
  // static uint16_t sLastMillis = 0;
  
  // uint16_t ms = millis();
  // uint16_t deltams = ms - sLastMillis ;
  
  // if(deltams>10000){
  //   flag_mode = 0;
  //   mode = (mode >1? 0: mode+1);
  // }
  // if(flag_mode == 0){
  //   sLastMillis  = millis();
  //   flag_mode = 1;
  // }

  switch(mode){
    case 0:{
      pride();
      break;
    }
    case 1:{
      rainbow();
      break;
    }
    case 2:{
      Fire2012();
      break;
    }
  }
  //OTA更新中断
  ArduinoOTA.handle();
  server.handleClient();
  //hello
}

